#include <windows.h>
#include <wininet.h>
#include <string>
#include <iostream>

#pragma comment(lib, "wininet.lib")

// 全局变量
HWND hwndEdit; // 文本框句柄

// 从 API 获取舔狗日记内容
std::string FetchContent() {
    HINTERNET hInternet = InternetOpenA("MyApp", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if (!hInternet) return "Failed to open internet session.";

    HINTERNET hUrl = InternetOpenUrlA(hInternet, "https://api.52vmy.cn/api/wl/yan/tiangou", NULL, 0, INTERNET_FLAG_RELOAD, 0);
    if (!hUrl) {
        InternetCloseHandle(hInternet);
        return "Failed to open URL.";
    }

    char buffer[1024];
    DWORD bytesRead;
    std::string content;

    while (InternetReadFile(hUrl, buffer, sizeof(buffer) - 1, &bytesRead) && bytesRead) {
        buffer[bytesRead] = '\0';
        content += buffer;
    }

    InternetCloseHandle(hUrl);
    InternetCloseHandle(hInternet);

    return content;
}

// 解析 JSON 内容
std::string ParseContent(const std::string& json) {
    size_t contentPos = json.find("\"content\":\"") + 11;
    size_t endPos = json.find("\"", contentPos);
    if (contentPos != std::string::npos && endPos != std::string::npos) {
        return json.substr(contentPos, endPos - contentPos);
    }
    return "Failed to parse content.";
}

// 窗口过程函数
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_COMMAND:
            if (LOWORD(wParam) == 1) { // 按钮点击事件
                std::string content = FetchContent();
                content = ParseContent(content);
                SetWindowTextA(hwndEdit, content.c_str());
            }
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

// 主函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    const char* CLASS_NAME = "Sample Window Class";

    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        "舔狗日记",                     // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
    );

    if (hwnd == NULL) {
        return 0;
    }

    // 创建按钮
    HWND hwndButton = CreateWindow(
        "BUTTON", "获取新的舔狗日记",
        WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
        10, 10, 200, 30,
        hwnd, (HMENU)1, hInstance, NULL
    );

    // 创建文本框
    hwndEdit = CreateWindow(
        "EDIT", "",
        WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_READONLY,
        10, 50, 380, 200,
        hwnd, NULL, hInstance, NULL
    );

    ShowWindow(hwnd, nCmdShow);

    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}